'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Comment extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      models.Comment.belongsTo(models.Article, {
        as: 'article',
      });
      models.Comment.belongsTo(models.User, {
        as: 'user'
      });
    }
  }
  Comment.init({
    content: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {
          msg: '评论内容必须存在'
        },
        notEmpty: {
          msg: '评论内容不能为空'
        },
        len: {
          args: [1, 255],
          msg: '评论内容长度必须在1-520个字符之间'
        }
      }
    },
    articleId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      validate: {
        notNull: {
          msg: '文章id必须存在'
        },
        isInt: {
          msg: '文章id必须为整数'
        }
      }
    },
    userId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      validate: {
        notNull: {
          msg: '用户id必须存在'
        },
        isInt: {
          msg: '用户id必须为整数'
        }
      }
    }
  }, {
    sequelize,
    modelName: 'Comment',
  });
  return Comment;
};